<?php
include 'redirectAdmin.php';
$action = "manage";
if (isset ( $_GET ["action"] )) {
	$action = $_GET ["action"];
}

?>

<table class="manage-service">
	<tr>
		<td class="left-menu">
			<div><a href="bookmarks.php?page=manageService&action=manage">Zarządzaj usługami</a></div><br/> 
			<div><a href="bookmarks.php?page=manageService&action=managePart">Zarządzaj częściami usług</a></div><br/>
			<div><a href="bookmarks.php?page=manageService&action=find">Odszukaj</a></div>
		</td>
		<td>
			
			<?php
			if ($action == "find") {
				
				$sid = null;
				if("remove" == ParamUtils::get("subaction") && ($sid = ParamUtils::get("sid")) != null){
					ServiceUtils::removeById($sid);
				}
				
				?>
			<div style = " margin-left: 20px;">
			<div><b>Wyszukiwanie usług</b></div><br/>	
			<div>
				<form action="bookmarks.php?page=manageService&action=find" method="post">
					<div>Nazwa</div>
					<input type="text" name="name" value="<?php echo ParamUtils::getString("name"); ?>"/>
					<div>Nazwa częsci</div>
					<input type="text" name="partName" value="<?php echo ParamUtils::getString("partName"); ?>"/>
					<div>Czy aktywna </div>
					<input type="checkbox" name="active" <?php echo ParamUtils::getBoolean("active") ? "CHECKED" : ""; ?>/></div>
					<div>Czy z promocją </div><input type="checkbox" name="promotion" <?php echo ParamUtils::getBoolean("promotion") ? "CHECKED" : ""; ?>/>
					<div>
						<div>Zawierajacy typ części</div><br/>  
						<select name="types[]" multiple="multiple">
							<option <?php echo ParamUtils::getBoolean("types", ServiceType::INT_SERVICE) ? "SELECTED" : ""; ?>" value="<?php echo ServiceType::INT_SERVICE;?>" > Internet  </option>						
							<option <?php echo ParamUtils::getBoolean("types", ServiceType::PHONE_SERVICE) ? "SELECTED" : ""; ?>" value="<?php echo ServiceType::PHONE_SERVICE;?>" > Telefon  </option>
							<option <?php echo ParamUtils::getBoolean("types", ServiceType::TV_SERVICE )  ? "SELECTED" : ""; ?>" value="<?php echo ServiceType::TV_SERVICE;?>" >  Telewizja </option>
						</select>
					</div>
					
					<input type="hidden" value="true" name="search" />
					<input type="submit" value="Szukaj" /> 
				</form>
			</div>
			</div>
			
			
			<?php 
				$result = null;
				if(isset($_POST['search'])){
						$controls = new ServiceSearchControls();
						$controls->setActive(ParamUtils::getBoolean("active"));
						$controls->setPromotion(ParamUtils::getBoolean("promotion"));
						$controls->setName(ParamUtils::get("name"));
						$controls->setPartName(ParamUtils::get("partName"));
						$types = ParamUtils::get("types");
						if($types != null){
							foreach($types as $type){
								$controls->addType($type);	
							}
						}
						
						$result = ServiceUtils::find($controls);
						
						
						if(is_array($result) && sizeof($result) > 0){
							?>
						<table> 
						<tr>
							<th>Id</th>
							<th>Nazwa</th>
							<th>Okres</th>
							<th>Cena</th>
							<th>Opis</th>
							<th>Części</th>
							<th>Akcje</th>
						</tr>
						<?php	foreach($result as $service):?>		
							<tr>
								<td><?php echo $service->getId(); ?></td>
								<td><?php echo $service->getName(); ?></td>
								<td><?php echo $service->getPeriod(); ?></td>
								<td><?php echo $service->getPrice(); ?></td>
								<td><?php echo $service->getDesc(); ?></td>
								<td>	
									<select>
									<?php $serviceParts = $service->getParts();
										foreach($serviceParts as $part){
											echo "<option> " . $part->getName() . "/" . ServiceType::convert($part->getType()) . " </option>"; 
										}
									?>
									</select>
								</td>
								<td>
									<a href="bookmarks.php?page=manageService&action=manage&sid=<?php echo $service->getId();?>">Zarządzaj</a>
									<a href="bookmarks.php?page=manageUserService&action=find&sid=<?php echo $service->getId();?>">Pokaż sprzedaż</a>
									<a href="bookmarks.php?page=manageService&action=find&sid=<?php echo $service->getId();?>&subaction=remove">Usun</a>
								</td>
							</tr>
							<?php endforeach; ?>
						</table> 
						<?php 							
						}			
				}
				?>
			<?php
			// ZARZADZANIE USLUGAMI 
			} else if($action=="manage"){
				
				$service = new Service();
				$pid = -1;
				$request = "add";
				if(isset($_GET['sid']) && VariableUtils::isNumber($_GET['sid'])){
								$service = ServiceUtils::get($_GET['sid']);
								$psc = new PromotionSearchControls();
								$psc->setSid($service->getId());
								$servicePromotion = PromotionUtils::find($psc);
								if($servicePromotion != null && sizeof($servicePromotion) > 0){
									$pid = $servicePromotion[0]->getId();
								}
								$request = "modify";				
				}else if(isset($_GET['request'])){
					
					$service->setName(ParamUtils::get("name"));
					$service->setPeriod(ParamUtils::get("period"));
					$service->setPrice(ParamUtils::get("price"));
					$service->setDesc(ParamUtils::get("desc"));
					$service->setActive(ParamUtils::getBoolean("active"));
					
					$partIds = ParamUtils::get("parts");
					if($partIds != null){
						foreach($partIds as $partId){
							$part = ServiceUtils::getServicePart($partId);
							$service->addPart($part);
						}
					}
					
					$pid = ParamUtils::get("promotion");
					
					if("add" == $_GET['request']){
						ServiceUtils::add($service);
					}else{
						$service->setId(ParamUtils::get("id"));
						ServiceUtils::modify($service);
						ServiceUtils::unAssignPromotion($service, null);
					}
					
					if($pid != null){
							ServiceUtils::assignPromotion($service, PromotionUtils::get($pid));
					}
					
					$request = "modify";
				}
				?>				
				<div style = "margin-left: 50px">
				<form action="bookmarks.php?page=manageService&action=manage<?php echo "&request=" . $request; ?>" method="post">
				
				<table>
					<tr>
							<th>Nazwa</th>
							<th>Okres</th>
							<th>Cena</th>
							<th>Opis</th>
							<th>Aktywna</th>
					</tr>
					<tr>
								<td><input type="text" name="name" value="<?php echo $service->getName(); ?>" /></td>
								<td><input type="text" name="period" value="<?php echo $service->getPeriod(); ?>" /></td>
								<td><input type="text" name="price" value="<?php echo $service->getPrice(); ?>" /></td>
								<td><input type="text" name="desc" value="<?php echo $service->getDesc(); ?>" /></td>
								<td><input type="checkbox" name="active" <?php echo $service->isActive() ? "CHECKED" : ""; ?> /></td>
					</tr>
					<tr>
						<td colspan="2"> 
							<fieldset class="manage-fieldset">
							<legend>Części serwisu</legend>
								<select size="10" multiple="multiple" name="parts[]" class="serviceParts"> 
									<?php foreach($service->getParts() as $part){
										echo "<option value=\"" . $part->getId() . "\">" . $part->getName() . "/" . ServiceType::convert($part->getType()) . "</option>";
									}	
									?>
								</select>
							</fieldset>
						</td>
						
						<td>
							<script type="text/javascript">
									jQuery(document).ready(function(){
										jQuery(".removeServicePart").click(function(event){
											removeSelected(jQuery(".serviceParts"));
										});

										jQuery(".addServicePart").click(function(event){
											copySelected(jQuery(".tvServiceParts"), jQuery(".serviceParts"));
											copySelected(jQuery(".intServiceParts"), jQuery(".serviceParts"));
											copySelected(jQuery(".phoneServiceParts"), jQuery(".serviceParts"));
										});
									});							
							</script>
							<p class="removeServicePart">Usun</p>
							<p class="addServicePart">Dodaj</p>
						</td>
						<td colspan="2"> 
							<fieldset class="manage-fieldset">
							<legend><?php echo ServiceType::convert(ServiceType::TV_SERVICE);?></legend>
									<select size="10" multiple="multiple" class="tvServiceParts"> 
											<?php 
												$partControls = new ServicePartSearchControls();
												$partControls->setType(ServiceType::TV_SERVICE);
												$tvServiceParts = ServiceUtils::findPart($partControls);
												foreach($tvServiceParts as $part){
													echo "<option value=\"" . $part->getId() . "\">" . $part->getName() ."</option>";
												}
											?>
									</select>
							</fieldset>
							
							<fieldset class="manage-fieldset">
							<legend><?php echo ServiceType::convert(ServiceType::INT_SERVICE);?></legend>
								<select size="10" multiple="multiple" class="intServiceParts"> 
										<?php 
											
											$partControls->setType(ServiceType::INT_SERVICE);
											$intServiceParts = ServiceUtils::findPart($partControls);
											foreach($intServiceParts as $part){
												echo "<option value=\"" . $part->getId() . "\">" . $part->getName() . "</option>";
											}
										?>
								</select>
							</fieldset>
							<fieldset class="manage-fieldset">
							<legend><?php echo ServiceType::convert(ServiceType::PHONE_SERVICE);?></legend>
								<select size="10" multiple="multiple" class="phoneServiceParts"> 
										<?php 
											$partControls->setType(ServiceType::PHONE_SERVICE);
											$phoneServiceParts = ServiceUtils::findPart($partControls);
											foreach($phoneServiceParts as $part){
												echo "<option value=\"" . $part->getId() . "\">" . $part->getName() .  "</option>";
											}
										?>
								</select>
							</fieldset>
						</td>
					</tr>
					
					<tr>
						<td colspan="5">
							<fieldset class="manage-fieldset">
								<legend>Promocja</legend>
								<select size="10" name="promotion">
									<?php 
										$promotions = PromotionUtils::find(new PromotionSearchControls());				
										foreach($promotions as $promotion){
											echo "<option value=\"" . $promotion->getId() . "\"" . (ParamUtils::getBoolean("promotion", $promotion->getId()) || ($pid != null && $pid == $promotion->getId()) ? "SELECTED" : "") .">" . $promotion->getName() . " : " . $promotion->getDateFrom()  . "/" . $promotion->getDateTo() . "</option>";
										}
									?>
								</select>
							</fieldset>
						</td>
					</tr>
					
				</table>
				<input type="hidden" name="id" value="<?php echo $service->getId();?>" />
				<input type="submit" value="Zapisz" onclick="selectOptions(jQuery('.serviceParts')); return true;" />
				</form>
				</div>
			<?php
			}else{
				// ZARZADZANIE CZESCIAMI
				
				$servicePart = new ServicePart();
				$servicePart->setId(ParamUtils::get("id"));
				$servicePart->setName(ParamUtils::get("name"));
				$servicePart->setType(ParamUtils::get("type"));
				$action = "add";
				if(isset($_POST['action'])){
					$action = $_POST['action'];
					if("add" == $action){
						ServiceUtils::addPart($servicePart);
						$action = "modify";						
					}else if("modify" == $action){
						ServiceUtils::modifyPart($servicePart);
					}else if("remove" == $action && VariableUtils::isNumber($servicePart->getId())){
						ServiceUtils::removePart($servicePart);
						$action = "add";
					}
				}
				
			?>
			
			<script type="text/javascript">
				function feelForm(className, tid){
					var part = jQuery("." + className + " option:selected");
					var partId = part.attr("value");
					var name = part.text();
					var form = jQuery(".partForm");
					
					form.find("input[name='name']").attr("value", name);
					form.find("option").removeAttr("selected");
					form.find("option[value="+tid+"]").attr("selected", "selected");
					form.find("input[name='id']").attr("value", partId);
					form.find("input[name='action']").attr("value", "modify");
					jQuery(".managePartActionName").text("Modyfikacja");
				}

				function changeActionToDelete(){
					var form = jQuery(".partForm");

					if(form.find("input[name='id']").attr("value").length > 0){
						form.find("input[name='action']").attr("value", "remove");
						return true;
					}else{
						alert("Empty input.");
						return false;
					}

				}

				function resetFormFields(){
					var form = jQuery(".partForm");
					form.find("input[name='name']").attr("value", "");
					form.find("input[name='id']").attr("value", "");
					form.find("input[name='action']").attr("value", "add");
					jQuery(".managePartActionName").text("Dodawanie");
				}

				jQuery(document).ready(function(){
						var action = jQuery(".partForm input[name='action']").attr("value");
						if("add" == action ){
							resetFormFields();
							jQuery(".managePartActionName").text("Dodawanie");
						}else if("modify" == action){
							jQuery(".managePartActionName").text("Modyfikacja");
						}
				});
			</script>
			<table>
			
				<tr>
						<td>
							<div class="managePartActionName"> &nbsp; </div>						
						</td>
				</tr>
				<tr>
					<td colspan="3">
						<form action="bookmarks.php?page=manageService&action=managePart" method="post" class="partForm">
							<input type="text" name="name" value="<?php echo $servicePart->getName(); ?>"/>
							<select name="type">
								<option <?php echo ParamUtils::getBoolean("type", ServiceType::INT_SERVICE) ? "SELECTED" : ""; ?> value="<?php echo ServiceType::INT_SERVICE;?>" class="int"> <?php echo ServiceType::convert(ServiceType::INT_SERVICE);?> </option>
								<option <?php echo ParamUtils::getBoolean("type", ServiceType::PHONE_SERVICE) ? "SELECTED" : ""; ?> value="<?php echo ServiceType::PHONE_SERVICE;?>" class="phone"> <?php echo ServiceType::convert(ServiceType::PHONE_SERVICE);?> </option>
								<option <?php echo ParamUtils::getBoolean("type", ServiceType::TV_SERVICE) ? "SELECTED" : ""; ?> value="<?php echo ServiceType::TV_SERVICE;?>" class="tv"> <?php echo ServiceType::convert(ServiceType::TV_SERVICE);?> </option>
							</select>
							<input type="hidden" value="<?php echo $servicePart->getId(); ?>" name="id" />
							<input type="hidden" value="<?php echo $action;?>" name="action" />
							<input type="submit" value="Zapisz" />
							<input type="submit" value="Usun" onclick="return changeActionToDelete();" /> 
							<input type="button" value="Wyczysc" onclick="resetFormFields(); return false;" />
						</form>
					</td>
				</tr>
				
				<tr>
					<td>
						<fieldset class="manage-fieldset">
							<legend><?php echo ServiceType::convert(ServiceType::INT_SERVICE);?></legend>
								<select size=10 class="intParts" onchange="feelForm('intParts', <?php echo ServiceType::INT_SERVICE; ?>);">
									<?php 
										
										$partControls = new ServicePartSearchControls();
										$partControls->setType(ServiceType::INT_SERVICE);
										$parts = ServiceUtils::findPart($partControls);
										foreach($parts as $part){
											echo "<option value=\"" . $part->getId() . "\">" . $part->getName() ."</option>";	
										}
									?>
								</select>
						</fieldset>
					</td>
					<td>
						<fieldset class="manage-fieldset">
							<legend><?php echo ServiceType::convert(ServiceType::TV_SERVICE);?></legend>
								<select size=10 class="tvParts" onchange="feelForm('tvParts', <?php echo ServiceType::TV_SERVICE; ?>);">
									<?php 
										$partControls->setType(ServiceType::TV_SERVICE);
										$parts = ServiceUtils::findPart($partControls);
										foreach($parts as $part){
											echo "<option value=\"" . $part->getId() . "\">" . $part->getName() ."</option>";	
										}
									?>
								</select>
								
								
						</fieldset>
					</td>
					<td>
						<fieldset class="manage-fieldset">
							<legend><?php echo ServiceType::convert(ServiceType::PHONE_SERVICE);?></legend>
								<select size=10 class="phoneParts" onchange="feelForm('phoneParts', <?php echo ServiceType::PHONE_SERVICE; ?>);">
									<?php 
										$partControls->setType(ServiceType::PHONE_SERVICE);
										$parts = ServiceUtils::findPart($partControls);
										foreach($parts as $part){
											echo "<option value=\"" . $part->getId() . "\">" . $part->getName() ."</option>";	
										}
									?>
								</select>
						</fieldset>
					</td>
				</tr>
			</table>
			<?php 
			}
			?>
		</td>
	</tr>
</table>

